package com.jyoume.yudao.generator;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;

@Mojo(name = "CodeGenerator",
        requiresProject = true,
        defaultPhase = LifecyclePhase.GENERATE_SOURCES,
        requiresDependencyResolution = ResolutionScope.COMPILE,
        threadSafe = true)
@Execute(phase = LifecyclePhase.GENERATE_SOURCES)
public class CodeGeneratorMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project}", readonly = true)
    protected MavenProject project;

    @Parameter(defaultValue = "jyoume", property = "company", required = true)
    private String company;
    @Parameter(defaultValue = "ui-admin", property = "uiProjectPath")
    private String uiProjectPath;

    /**
     * 业务模块名称，例如：系统模块
     */
    @Parameter(defaultValue = "system", property = "module")
    private String bizModule;

    @Parameter(defaultValue = "jdbc:postgresql://127.0.0.1:5432/petbox", property = "databaseUrl", required = true)
    private String databaseUrl;

    @Parameter(defaultValue = "postgres", property = "dbUsername", required = true)
    private String dbUsername;
    @Parameter(defaultValue = "postgres", property = "dbPassword", required = true)
    private String dbPassword;
    @Parameter(defaultValue = "t_", property = "tablePrefix")
    private String tablePrefix;
    @Parameter(property = "tableNames", required = true)
    private String tableNames;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("生成代码中...");
        //指定业务模块名称，例如：系统模块
        CodeGenerator.company = company;
        //前端UI项目所在目录
        CodeGenerator.uiProjectPath = uiProjectPath;
        CodeGenerator.excelCodeGenerate = false;
        //CodeGenerator.appControllerCodeGenerate = true;
        //sys_org表是树形结构表
        CodeGenerator.isTreeTable = false;
        CodeGenerator.lazyTree = false;
        CodeGenerator.databaseUrl = databaseUrl;
        CodeGenerator.username = dbUsername;
        CodeGenerator.password = dbPassword;
        CodeGenerator.tablePrefix = tablePrefix;

        new CodeGenerator(getLog(), project.getName()).generator(bizModule, tableNames.split(","));
        getLog().info("生成代码完毕...");
    }
}
